package allen.ui.log;

import java.awt.BorderLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.table.TableRowSorter;

import allen.log.LogDataCenter;
import allen.log.LogLevel;

/**
 * Log filter setting panel.
 * */
class FilterSettingPanel extends JPanel {

	private static final long serialVersionUID = 1L;

	private LogEntityFilter filter;

	private JLabel logNameLabel = new JLabel("Log Name", SwingConstants.CENTER);
	private JComboBox logNameComboBox;

	private JLabel logLevelLabel = new JLabel("Log Level",
			SwingConstants.CENTER);
	private JComboBox logLevelComboBox;

	private void initLogNameComboBox() {
		logNameComboBox = new JComboBox(
				new String[] { LogEntityFilter.AllLogNameTag });
		logNameComboBox.setMaximumSize(LogConstant.DefaultComboSize);
	}

	private void initLogLevelComboBox() {
		logLevelComboBox = new JComboBox(LogLevel.values());
		logLevelComboBox.setMaximumSize(LogConstant.DefaultComboSize);
	}

	private void addLogNamePanel() {
		JPanel logNamePanel = new JPanel(new BorderLayout());

		logNameLabel.setPreferredSize(LogConstant.DefaultLabelSize);
		logNamePanel.add(logNameLabel, BorderLayout.WEST);

		initLogNameComboBox();
		logNamePanel.add(logNameComboBox, BorderLayout.CENTER);

		this.add(logNamePanel, BorderLayout.CENTER);
	}

	private void addOtherPanel() {
		JPanel otherPanel = new JPanel();
		BoxLayout boxLayout = new BoxLayout(otherPanel, BoxLayout.X_AXIS);
		otherPanel.setLayout(boxLayout);

		logLevelLabel.setPreferredSize(LogConstant.DefaultLabelSize);
		otherPanel.add(logLevelLabel);

		initLogLevelComboBox();
		otherPanel.add(logLevelComboBox);

		otherPanel.add(Box.createHorizontalGlue());

		this.add(otherPanel, BorderLayout.SOUTH);
	}

	private void init() {

		this.setLayout(new BorderLayout());
		this.setBorder(BorderFactory
				.createTitledBorder(LogConstant.FilterTitle));

		addLogNamePanel();
		addOtherPanel();
	}

	private void addListeners() {

		logNameComboBox.addFocusListener(new FocusListener() {
			@Override
			public void focusGained(FocusEvent e) {
				logNameComboBox.removeAllItems();
				logNameComboBox.addItem(LogEntityFilter.AllLogNameTag);

				String[] names = LogDataCenter.logDataCenter
						.getLogEntityNames();
				for (String name : names) {
					logNameComboBox.addItem(name);
				}
			}

			@Override
			public void focusLost(FocusEvent e) {
			}

		});

		logNameComboBox.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == ItemEvent.SELECTED) {
					filter.setLogName((String) logNameComboBox
							.getSelectedItem());
				}

			}
		});

		logLevelComboBox.addItemListener(new ItemListener() {

			@Override
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == ItemEvent.SELECTED) {
					filter.setLogLevel((LogLevel) logLevelComboBox
							.getSelectedItem());
				}
			}
		});

	}

	FilterSettingPanel(TableRowSorter<LogDataCenter> sorter) {
		filter = LogEntityFilter.createFilterAndAddToSorter(sorter);
		init();
		addListeners();
	}

}
